package com.godyao.mall.order.controller.front;

import cn.hutool.core.util.ArrayUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.godyao.mall.core.model.ResultMsg;
import com.godyao.mall.core.model.goods.Goods;
import com.godyao.mall.goods.api.feign.GoodsFeign;
import com.godyao.mall.order.model.Order;
import com.godyao.mall.order.model.req.OrderReq;
import com.godyao.mall.order.service.IOrderService;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.persistence.criteria.Predicate;
import java.util.ArrayList;
import java.util.List;

/**
 * @author godyao
 * @date 2022/4/1
 */
@RestController
@RequiredArgsConstructor
@RequestMapping(value = "/front")
public class OrderFrontController {
    private final IOrderService orderService;

    private final GoodsFeign goodsFeign;

    @GetMapping("/order/goods/{goodsId}")
    public ResultMsg<Goods> findOrderGoodsById(@PathVariable(value = "goodsId") Long id) {
        return goodsFeign.findGoodsByGoodsId(id);
    }
    @GetMapping("/order/{orderId}")
    public ResultMsg<Order> findOrderById(@PathVariable(value = "orderId") Long id) {
        return ResultMsg.resultSuccess(orderService.findOrderById(id));
    }

    @GetMapping("/order")
    public ResultMsg<Page<Order>> findPagedGoods(OrderReq orderReq) {
        final Specification<Object> specification = (root, query, criteriaBuilder) -> {
            final List<Predicate> predicates = new ArrayList<>();
            if (ObjectUtil.isNotNull(orderReq) && StrUtil.isNotBlank(orderReq.getGoodsName())) {
                final Predicate likeGoodsName = criteriaBuilder.like(root.get("goodsName").as(String.class), "%" + orderReq.getGoodsName() + "%");
                predicates.add(likeGoodsName);
            }
            if (ObjectUtil.isNotNull(orderReq) && ObjectUtil.isNotNull(orderReq.getId())) {
                final Predicate eqId = criteriaBuilder.equal(root.get("id").as(Long.class), orderReq.getId());
                predicates.add(eqId);
            }
            if (ObjectUtil.isNotNull(orderReq) && ObjectUtil.isNotNull(orderReq.getStatus())) {
                final Predicate eqStatus = criteriaBuilder.equal(root.get("status").as(Integer.class), orderReq.getStatus());
                predicates.add(eqStatus);
            }
            return criteriaBuilder.and(ArrayUtil.toArray(predicates, Predicate.class));
        };

        return ResultMsg.resultSuccess(orderService.findAll(specification, PageRequest.of(orderReq.getPageNumber()-1, orderReq.getPageSize())));
    }
}
